/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vates.curso.dao;

import java.util.HashSet;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.vates.curso.entities.Phone;
import com.vates.curso.entities.Role;
import com.vates.curso.entities.User;

public class Main {

	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext(
				"/beans.xml");

		RoleDao roleDao = (RoleDao) context.getBean("roleDao");
		UserDao userDao = (UserDao) context.getBean("userDao");
		PhoneDao phoneDao = (PhoneDao) context.getBean("phoneDao");

		// Creo y guardo un user
		User user = new User();
		user.setUserPhone(new HashSet<Phone>());
		user.setUserRole(new HashSet<Role>());
		user.setUserFirstName("lucas");
		user.setUserLastName("gonzales");

		user = userDao.save(user);

		User user1 = userDao.get(user.getUserId());
		user1.getUserBarrio();
//		for (Phone phone : user1.getUserPhone()) {
//			
//			System.out.println(phone.getPhoneNumber());
//			
//		}
		// Creo y guardo un telefono
		Phone phone = new Phone();
		phone.setPhoneNumber("21212");
		phone.setPhoneType("mobile");
		phone.setPhoneUser(user);

		// Creo y guardo un telefono
		Phone phone2 = new Phone();
		phone2.setPhoneNumber("421212");
		phone2.setPhoneType("home");
		phone2.setPhoneUser(user);

		// Creo y guardo un rol
		Role role = new Role();
		role.setRoleDescription("user");
		role.setRoleName("user");
		role.setRoleUser(user);

		role = roleDao.save(role);
		phone = phoneDao.save(phone);
		phone2 = phoneDao.save(phone2);

		user.setUserPhone(new HashSet<Phone>());
		user.getUserPhone().add(phone);
		user.getUserPhone().add(phone2);
		user.setUserRole(new HashSet<Role>());
		user.getUserRole().add(role);

		
		
		// user = userDao.save(user);

		// phoneDao.save(phone);

		role.setRoleUser(user);

		// Persona personaObtenida = personaDao.get(persona.getId());

		System.out.println(user.toString());
		for (Phone phone3 : user.getUserPhone()) {
			System.out.println(" Phone number: " + phone3.getPhoneNumber());
			System.out.println(" Phone user: "
					+ phone3.getPhoneUser().getUserId());
		}

		User user2= userDao.get((long) 1);
		
		
		for (Phone phone24: user2.getUserPhone()) {
			System.out.println("TELEFONO: " + phone24.getPhoneNumber());
		}
	}

}
